<html>
<head><meta charset="utf-8"><title>Can I make an `impl Add&lt;..&gt; for MyType` panic in `std`? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Can.20I.20make.20an.20.60impl.20Add.3C.2E.2E.3E.20for.20MyType.60.20panic.20in.20.60std.60.3F.html">Can I make an `impl Add&lt;..&gt; for MyType` panic in `std`?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="228428133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Can%20I%20make%20an%20%60impl%20Add%3C..%3E%20for%20MyType%60%20panic%20in%20%60std%60%3F/near/228428133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Iago-lito <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Can.20I.20make.20an.20.60impl.20Add.3C.2E.2E.3E.20for.20MyType.60.20panic.20in.20.60std.60.3F.html#228428133">(Mar 02 2021 at 15:26)</a>:</h4>
<p>@t-lib Hey there, I'm thinking about <code>impl Add&lt;u*&gt; for NonZeroU*</code> in <code>std</code>. This could be safe in debug mode because <code>Add</code> panics on overflow, but it would be unsafe in release mode because the nonzero invariant could be broken on overflow. Is it okay that to make <code>impl Add&lt;u*&gt; for NonZeroU*</code> panic <em>also</em> in release mode instead? Or is it something an <code>impl Add</code> should explicitely never do?</p>



<a name="228429004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Can%20I%20make%20an%20%60impl%20Add%3C..%3E%20for%20MyType%60%20panic%20in%20%60std%60%3F/near/228429004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Iago-lito <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Can.20I.20make.20an.20.60impl.20Add.3C.2E.2E.3E.20for.20MyType.60.20panic.20in.20.60std.60.3F.html#228429004">(Mar 02 2021 at 15:31)</a>:</h4>
<p>Wops, and I'm not even sure this is the right way to "ping the T-lib team" as I have been suggested to, how do I "ping the T-lib team"? :(</p>



<a name="228429697"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Can%20I%20make%20an%20%60impl%20Add%3C..%3E%20for%20MyType%60%20panic%20in%20%60std%60%3F/near/228429697" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Iago-lito <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Can.20I.20make.20an.20.60impl.20Add.3C.2E.2E.3E.20for.20MyType.60.20panic.20in.20.60std.60.3F.html#228429697">(Mar 02 2021 at 15:34)</a>:</h4>
<p>Okay sorry, attempting to move that to the right channel X)</p>



<a name="228437942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Can%20I%20make%20an%20%60impl%20Add%3C..%3E%20for%20MyType%60%20panic%20in%20%60std%60%3F/near/228437942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Can.20I.20make.20an.20.60impl.20Add.3C.2E.2E.3E.20for.20MyType.60.20panic.20in.20.60std.60.3F.html#228437942">(Mar 02 2021 at 16:19)</a>:</h4>
<p>it wouldn't even quite be safe in debug mode, because the arithmetic overflow is actually a flag of its own, and it can always be on or off.</p>



<a name="228438120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Can%20I%20make%20an%20%60impl%20Add%3C..%3E%20for%20MyType%60%20panic%20in%20%60std%60%3F/near/228438120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Can.20I.20make.20an.20.60impl.20Add.3C.2E.2E.3E.20for.20MyType.60.20panic.20in.20.60std.60.3F.html#228438120">(Mar 02 2021 at 16:20)</a>:</h4>
<p>i am not on amy team, but personally i advise you to not write this code.</p>



<a name="228438655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Can%20I%20make%20an%20%60impl%20Add%3C..%3E%20for%20MyType%60%20panic%20in%20%60std%60%3F/near/228438655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Iago-lito <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Can.20I.20make.20an.20.60impl.20Add.3C.2E.2E.3E.20for.20MyType.60.20panic.20in.20.60std.60.3F.html#228438655">(Mar 02 2021 at 16:22)</a>:</h4>
<p>Mh, interesting, does this also hold for <code>checked_add</code> <em>etc.</em>? If not, <code>impl Add&lt;u*&gt; for NonZeroU*</code> would simply defer to <code>u*::checked_add</code> and explicitly panic if the return value is <code>None</code>.. unless the flag you're referring to also affects <code>checked_add</code> :\</p>



<a name="228440876"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Can%20I%20make%20an%20%60impl%20Add%3C..%3E%20for%20MyType%60%20panic%20in%20%60std%60%3F/near/228440876" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Iago-lito <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Can.20I.20make.20an.20.60impl.20Add.3C.2E.2E.3E.20for.20MyType.60.20panic.20in.20.60std.60.3F.html#228440876">(Mar 02 2021 at 16:36)</a>:</h4>
<p>Well, in other terms, I'd force the <code>Add</code> implementation to be checked even though users do not expect it. Then they'd better directly use <code>checked_add</code>instead and be more explicit. Alright, let's forget about it. Thank you :)</p>



<a name="228455679"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Can%20I%20make%20an%20%60impl%20Add%3C..%3E%20for%20MyType%60%20panic%20in%20%60std%60%3F/near/228455679" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Can.20I.20make.20an.20.60impl.20Add.3C.2E.2E.3E.20for.20MyType.60.20panic.20in.20.60std.60.3F.html#228455679">(Mar 02 2021 at 17:58)</a>:</h4>
<p>checked_add is always checked</p>



<a name="228475493"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Can%20I%20make%20an%20%60impl%20Add%3C..%3E%20for%20MyType%60%20panic%20in%20%60std%60%3F/near/228475493" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Can.20I.20make.20an.20.60impl.20Add.3C.2E.2E.3E.20for.20MyType.60.20panic.20in.20.60std.60.3F.html#228475493">(Mar 02 2021 at 20:01)</a>:</h4>
<p>Given the existing ones are cases where it's always fine <code>impl BitOr&lt;u32&gt; for NonZeroU32</code>, I think people would expect that for all the ops.  My intuition here is that if it's not always safe, it just should be done on <code>.get()</code> instead to be clear about that.</p>



<a name="228502599"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Can%20I%20make%20an%20%60impl%20Add%3C..%3E%20for%20MyType%60%20panic%20in%20%60std%60%3F/near/228502599" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Can.20I.20make.20an.20.60impl.20Add.3C.2E.2E.3E.20for.20MyType.60.20panic.20in.20.60std.60.3F.html#228502599">(Mar 02 2021 at 23:00)</a>:</h4>
<p>Although, come to think of it, <span class="user-mention silent" data-user-id="335110">Iago-lito</span>, that does imply that <code>NonZeroU32::saturating_(add|mul)</code> might be fine.  I don't know what the RHS argument type should be, though, since there's no a trait for it like there is for <code>BitOr</code> and friends, and <code>impl Into&lt;u32&gt;</code> is too broad...</p>



<a name="228504597"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Can%20I%20make%20an%20%60impl%20Add%3C..%3E%20for%20MyType%60%20panic%20in%20%60std%60%3F/near/228504597" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Can.20I.20make.20an.20.60impl.20Add.3C.2E.2E.3E.20for.20MyType.60.20panic.20in.20.60std.60.3F.html#228504597">(Mar 02 2021 at 23:15)</a>:</h4>
<p>but why is it too broad</p>



<a name="228512186"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Can%20I%20make%20an%20%60impl%20Add%3C..%3E%20for%20MyType%60%20panic%20in%20%60std%60%3F/near/228512186" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Can.20I.20make.20an.20.60impl.20Add.3C.2E.2E.3E.20for.20MyType.60.20panic.20in.20.60std.60.3F.html#228512186">(Mar 03 2021 at 00:22)</a>:</h4>
<p>Because it includes things like <code>NonZeroU64 + u8</code>, which is inconsistent with the "same width" restrictions on all the other <code>saturating_add</code>s.  And also it'd make <code>NonZeroU32 + 4</code> give an error because it'd pick <code>i32</code> for the <code>4</code>.</p>



<a name="228516967"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Can%20I%20make%20an%20%60impl%20Add%3C..%3E%20for%20MyType%60%20panic%20in%20%60std%60%3F/near/228516967" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Can.20I.20make.20an.20.60impl.20Add.3C.2E.2E.3E.20for.20MyType.60.20panic.20in.20.60std.60.3F.html#228516967">(Mar 03 2021 at 01:10)</a>:</h4>
<p>plz fix the default type rules in e2024</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>